package shangguigu_juc.lock;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class ThreadDemo4 {

    public static void main(String[] args) {

        //List<String> list = new ArrayList<>(); // ConcurrentModificationException
        //List<String> list = new Vector<>(); // 解决方案一
        //List<String> list = Collections.synchronizedList(new ArrayList<>()); // 解决方案二
        List<String> list = new CopyOnWriteArrayList<>(); // 解决方案三

        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 向集合中添加内容
                    list.add(Thread.currentThread().getName()+"---"+UUID.randomUUID().toString().substring(0,5));
                    // 获取集合中的内容
                    System.out.println(list);
                }
            },String.valueOf(i)).start();
        }
    }
}
